<?php
namespace Admin\Controller;
use Think\Controller;
/**
 * 内容模型管理控制器
 */
 class ContentModelController extends IsLoginController{
 	/**
	 * 模型管理首页
	 */
	 public function index(){
	 	//查询模型表 
	 	$modelData = M('model')->select();
		$this->assign('modelData',$modelData);
	 	$this->display();	 	
	 }
	
	/**
	 * 添加模型
	 */
	 public function add(){
		$contentModel = D('model');
	 	if(IS_POST){	 		
	 		if(!$contentModel->addContentModel()){
	 			$this->error($contentModel->getError());
	 		}else{
	 			$this->success('模型添加成功');
	 		}			
	 	}
	 	$this->display();
	 }
	 
	 /**
	  * 添加字段
	  */
	  public function addField(){
	  	//获取模型表名
	  	$mname = I('get.mname');
		$contentModel = D('model');
	 	if(IS_POST){	 		
	 		if(!$contentModel->addField($mname)){ // echo '<hr />'.$contentModel->getError();
	 			$this->error($contentModel->getError());
	 		}else{ //echo 'ok';
	 			$this->success('模型添加成功');
	 		}
			
	 	}
	  	$this->display();
	  }
	  
	  
	  /**
	  * 修改字段
	  */
	  public function editField(){	  	
	  	$mname = I('get.mname');  					//获取模型表名
	  	$field = I('get.field');					//获取字段名
		$contentModel = D('model');
	 	if(IS_POST){	 		
	 		if(!$contentModel->editField($mname,$field)){ // echo '<hr />'.$contentModel->getError();
	 			$this->error($contentModel->getError());
	 		}else{ //echo 'ok';
	 			$this->success('模型修改成功');
	 		}
			
	 	}
	  	//查询当前模型下字段
		$sql = "SHOW FULL FIELDS FROM duosute_".$mname;
		$modelField = M()->query($sql);
		//组合表明  表单输入类型  默认值
		foreach($modelField as $k =>$v){
				if($v['field'] == $field){ //取出符合字段名的值 其余值清空
				  //获取input 文字  //获取input 类型	
					$Fname = strstr($v['comment'],'|',true );//提示文字
					//后部截取
					$afterword = ltrim(strstr ($v['comment'],'|'),'|');
					//输入框类型
					$Fword = strstr($afterword,'|',ture );
					//默认值
					$defaultValue= trim(strstr($afterword,'|' ),'|'); 
					
				    $modelField[$k]['fieldName'] = $Fname;
				 	$modelField[$k]['Fword'] = $Fword;
				 	$modelField[$k]['defaultValue'] = $defaultValue;
				}else{
					$modelField[$k] = null;
				}
		}		
		$modelField  = current(array_filter($modelField)); 				//过滤空值 并将多为转一维数组		
		//重组字段类型 和默认长度
		if(strchr($modelField['type'],'(')){   								//判断匹配  分离值
			$strLength = strchr($modelField['type'],'(');					//匹配“（”开始后边的字段长度值
			$strLength = intval(str_ireplace(array('(',')'), '', $strLength));		//除去字符串前后括号
			$strLength = $strLength == 0 ? '':$strLength;
			$type = strstr($modelField['type'],'(',true); 				//截取字符串类型值
			$modelField['strLength'] = $strLength;
			$modelField['strType']   = $type;
		}else{															//匹配不到情况
			$modelField['strType']   = $modelField['type'];				//没有长度 直接赋值类型
		}

		$this->assign('modelField',$modelField);
	  	$this->display();
	  }
	  
	  
	  /**
	   * 模型管理
	   */
	  public function editModel(){
	  	$modelName  = I("get.mname");
		//修改更新模型
		$contentModel = D('model');
		if(IS_POST){	 		
	 		if(!$contentModel->editContentModel($modelName)){
	 			$this->error($contentModel->getError());
	 		}else{
	 			$this->success('模型更新成功');
	 		}			
	 	}
	  	//查询当前模型
	  	$modelData = M('model')->where("mmodel_name='{$modelName}'")->find();
	  	//查询当前模型下字段
		$sql = "SHOW FULL FIELDS FROM duosute_".$modelName;
		$modelField = M()->query($sql);
		//组合表明  表单输入类型  默认值
		foreach($modelField as $k =>$v){
			  //获取input 文字  //获取input 类型	
				$Fname = strstr($v['comment'],'|',true );//提示文字
				//后部截取
				$afterword = ltrim(strstr ($v['comment'],'|'),'|');
				//输入框类型
				$Fword = strstr($afterword,'|',ture );
				//默认值
				$defaultValue= trim(strstr($afterword,'|' ),'|'); 
				
			    $modelField[$k]['fieldName'] = $Fname;
			 	$modelField[$k]['Fword'] = $Fword;
			 	$modelField[$k]['defaultValue'] = $defaultValue;
		}
	  	$this->assign('modelData',$modelData);
		$this->assign('modelField',$modelField);
	  	$this->display();
	  	
	  }

	/**
	 * 删除模型
	 */
	public function del(){
		$modelName =  I('get.name');
		$model = str_ireplace('onmodel', '', $modelName);
		//删除表
		$sql = "drop table duosute_".$modelName;
		$modelField = M()->query($sql);
		//删除模型记录
		M('model')->where("mfield ='{$model}'")->delete();
		$this->success('删除成功');
	}
 }
